package cn.b.sky.mes.pp.service.impl;

import java.util.Date;
import java.util.List;

import cn.b.sky.mes.pp.dao.ProductionOrderPlanDao;
import cn.b.sky.mes.pp.model.ProductionOrderPlan;
import cn.b.sky.mes.pp.model.condition.ProductionOrderPlanCondition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.common.model.Page;
import cn.b.sky.common.utils.DateUtils;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.mes.pp.service.ProductionOrderPlanService;

/**
 * 
 * <strong>Title : ProductionOrderPlanServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.pp.service.impl<br></strong>
 * <strong>Description : </strong>订单计划业务实现类<br> 
 * <strong>Create on : 2016年1月20日 上午10:25:54<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service
public class ProductionOrderPlanServiceImpl implements ProductionOrderPlanService {
  @Autowired
  ProductionOrderPlanDao productionOrderPlanDao;

  @Override
  public List<ProductionOrderPlan> selectUncompletedOrdersByOrderNums(String[] orderNums, String siteCode,
                                                                      String languageCode) {
    return productionOrderPlanDao.selectUncompletedOrdersByOrderNums(orderNums, siteCode, languageCode);
  }

  @Override
  public List<ProductionOrderPlan> selectProductionOrderPlansByDate(String[] orderNums, String siteCode,
      String languageCode, String startDate, String endDate) {
    return productionOrderPlanDao.selectProductionOrderPlansByDate(orderNums, siteCode, languageCode, startDate,
        endDate);
  }

  @Override
  public void saveOrderPlan(ProductionOrderPlan orderPlan) {
    productionOrderPlanDao.insertOrderPlan(orderPlan);
  }

  @Override
  public void deleteOrderPlanByOrderNum(String siteCode, String orderNum) {
    productionOrderPlanDao.deleteOrderPlanByOrderNum(siteCode, orderNum);
  }

  @Override
  public void deleteOrderPlanByOrderNumAndStartDate(String siteCode, String orderNum, String startDate) {
    productionOrderPlanDao.deleteOrderPlanByOrderNumAndStartDate(siteCode, orderNum, startDate);
  }

  @Override
  public int selectScheduleDays() {
    Integer scheduleDays = productionOrderPlanDao.selectScheduleDays(SecurityContextUtil.getSiteCode());
    if (null != scheduleDays) {
      return scheduleDays;
    }
    return -1;
  }

  @Override
  public Page selectProductionOrderPlansWithExtInfoPageByCondition(ProductionOrderPlanCondition condition) {
    DateUtils dateUtils = DateUtils.getDateUtils(DateUtils.YYYY_MM_DD);
    Date today = new Date();
    if (StringUtils.isBlank(condition.getPlannedStartDate())) {
      condition.setPlannedStartDate(dateUtils.format(today));
    }
    if (StringUtils.isBlank(condition.getPlannedEndDate())) {
      condition.setPlannedEndDate(dateUtils.format(today));
    }
    if (null == condition.getSiteCode()) {
      condition.setSiteCode("");
    }

    condition.setData(this.productionOrderPlanDao.selectProductionOrderPlansWithExtInfoByCondition(condition));
    condition.setTotalCount(this.productionOrderPlanDao.getProductionOrderPlansCountByCondition(condition));
    return condition;
  }

  @Override
  public List<ProductionOrderPlan> selectProductionOrderPlansWithExtInfoByCondition(
      ProductionOrderPlanCondition condition) {
    if (null == condition.getSiteCode()) {
      condition.setSiteCode("");
    }
    //代表查询全部
    condition.setPageNum(1);
    condition.setEndRow(-1);
    return this.productionOrderPlanDao.selectProductionOrderPlansWithExtInfoByCondition(condition);
  }

  @Override
  public ProductionOrderPlan getProductionOrderPlanWithExtInfoByPK(String siteCode, String id) {
    return this.productionOrderPlanDao.getProductionOrderPlanWithExtInfoByPK(null == siteCode ? "" : siteCode, id);
  }

  @Override
  public List<ProductionOrderPlan> selectArrangedOrdersByOrderNums(String siteCode, String[] orderNums, String languageCode) {
    return productionOrderPlanDao.selectArrangedOrdersByOrderNums(siteCode, orderNums, languageCode);
  }

}
